LeetCode09
LeetCode 第09题的分析和总结
题目描述:
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example 1:
Input: 121
Output: true
Example 2:
Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Follow up:
Coud you solve it without converting the integer to a string?
思路:
不转化为String类型,就直接的翻转过来比较两个值是否一样即可。
注意翻转过程中的integer 越界问题,是不需要考虑的。
代码:
public boolean isPalindrome(int num) {
if (num < 0 || (x!=0 && x%10==0)) {
return false;
}
int sum = num;
int temp = 0;
while (sum != 0) {
temp = temp * 10 + sum % 10;
sum = sum / 10;
}
return temp == num;
}
- 上一篇 JDK8的新特性2
- 下一篇 LeetCode11